const constants   = require('../../data/constants');
const deviceModel = require('../../model/device');
const server      = require('../../sip/server');

let Device = {
    list:function(req,res){
        deviceModel.list(req.query.account).then((rows)=>{
            // TODO:生成 hls 地址
            let handleRows=[];

            for(var i in rows){
                if(rows.length>1 && rows[i].deviceid == rows[i].parentid){
                    // nvr 的这个通道不用
                    continue;
                }
                rows[i].hls = constants.zlmedia.remote + constants.zlmedia.app + '/v_' + rows[i].parentid + '_' + rows[i].deviceid + '/hls.m3u8';
                rows[i].hlsTls =  constants.zlmedia.remoteTls + constants.zlmedia.app + '/v_' + rows[i].parentid + '_' + rows[i].deviceid + '/hls.m3u8';
                rows[i].rtmp= constants.zlmedia.rtmp  + constants.zlmedia.app + '/v_' + rows[i].parentid + '_' + rows[i].deviceid ;
                handleRows.push(rows[i]);
            }
            // 处理逻辑 ...
            let ret =Object.assign(constants.httpCode.OK , {data:{items:handleRows,total:handleRows.length}});
            res.send(ret);
        });
    },
    bye:function(req,res){
        console.log('bye req',req.query);
        let deviceid = req.query.deviceid;
        let channel  = req.query.channel;
        server.bye(deviceid,channel,(ret)=>{
            if(ret){
                res.send({'code':0,'msg':'success'});
            }else{
                res.send({'code':1,'msg':'通道未注册成功'});
            }
        })
    }
};
module.exports=Device;
